import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 周斌
 * Date: 2024-06-29
 * Time: 9:21
 */
public class Main {
    public int largestPerimeter(int[] nums) {
        //1.升序排序
        Arrays.sort(nums);
        int n = nums.length;
        //2.先将数组的最后一个元素当作最长的边进行遍历，如果这个最长的边找不到符合构成三角形的最大两条边，则就此时最长边的前一位作为最长边
        //依次循环。
        for (int i = n-1; i >= 2 ; i--) {
            //找到满足三角形的剩余两条边
            if((nums[i-1] + nums[i-2]) > nums[i]) {
                //满足条件直接返回三边之和
                return (nums[i] + nums[i-1] + nums[i-2]);
            }
        }
        return 0;
    }
    public static void main(String[] args) {
        Main main = new Main();
        int[] array = {2,1,2};
        int[] array1 = {1,2,1,10};
        int[] array2 = {3,2,3,4};
        System.out.println(main.largestPerimeter(array));
        System.out.println(main.largestPerimeter(array1));
        System.out.println(main.largestPerimeter(array2));
    }
}
